package com.happypets.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.happypets.daoImpl.PetDaoImpl;

public class LocalServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public LocalServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		
		
		PrintWriter out = response.getWriter();

		
		String id = request.getParameter("key");
		String sql = "select t.title,t.id from region t where t.parentid = "+id;
		
		PetDaoImpl dao = new PetDaoImpl();
		
		System.out.println(sql);
		
		List<Map<String,String>> ls = (List<Map<String, String>>) dao.query(sql);
		
		Gson gson=new Gson();
		String json=gson.toJson(ls);
//		System.out.println(json);
		out.write(json);
		
		out.flush();
		out.close();
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
